<?php

require_once("./Model/LoginModel.php");
require_once("./Model/User.php");

class LoginView {
	
	/**
	 * @var LoginModel
	 */
	private $loginModel;
	
	/**
	 * @var NavigationView
	 */
	private $navView;
	
	/**
	 * String for input username.
	 * @var String
	 */
	private static $inputUsername = "UsernameID";
	
	/**
	 * String for input password.
	 * @var String
	 */
	private static $inputPassword = "PasswordID";
	
	 /** 
	 * String for checkbox "Save login data".
	 * @var String
	 */
	private static $saveLoginData = "SaveLoginData";
	
	/**
	 * @var String Cookie name for Username
	 */
	private static $CookieUsername = "CookieUsername";
	
	/**
	 * @var String Cookie name for Password
	 */
	private static $CookiePassword = "CookiePassword";
	
	/**
	 * URL string for when user wants to logout.
	 * @var String
	 */
	private static $logoutURL = "logout";
	
	/**
	 * URL string for when user wants to login.
	 * @var String
	 */
	private static $loginURL = "login";
	
	/**
	 * @var String
	 */
	private $loginMessage = "";
	
	/**
	 * @var String
	 */
	private $username = "";
	
	/**
	 * @var String
	 */
	private $password = "";
	
	
	/**
	 * @param NavigationView $navigationView
	 */
	public function __construct(NavigationView 	$navigationView) {
		$this->navView = $navigationView;
	}
								
	/**
	 * @return String HTML
	 */
	public function getLoginHTML() {
		return "<h2>Ej inloggad</h2>
				<form action='?".$this->navView->getLoginURL()."' method='post'>
					<fieldset>
						<legend>Login - Skriv in användarnamn och lösenord</legend>
						<p>$this->loginMessage</p>
						<label for='".self::$inputUsername."'>Namn:</label>
						<input type='text' name='".self::$inputUsername."' id='".self::$inputUsername."' value='$this->username' />
						<label for='".self::$inputPassword."'>Lösenord:</label>
						<input type='text' name='".self::$inputPassword."' id='".self::$inputPassword."' />
						<label for='".self::$saveLoginData."'>Håll mig inloggad:</label>
						<input type='checkbox' name='".self::$saveLoginData."' id='".self::$saveLoginData."'/>
						<input type='submit' value='Logga in' />
					</fieldset>
				</form>";
	}
	
	/**
	 * @return String HTML
	 */
	public function getLogoutHTML ($username) {
		return "<h2>$username inloggad</h2>
				<p>$this->loginMessage</p>
				<a href='?".$this->navView->getLogoutURL()."'>Logga ut</a>";
	}
	
	/**
	 * Messages when trying logging in user.
	 */
	public function userAuthed($authed) {
		if ($authed) {
			if ($this->saveLoginData()) {
				$this->loginMessage = "Inloggning lyckades och vi kommer ihåg dig nästa gång";
			}
			else {
				if ($this->isCookiesSet()) {
					$this->loginMessage = "Inloggning lyckades via cookies";
				}
				else {
					$this->loginMessage = "Inloggning lyckades!";
				}
			}
		}
		else {
			if ($this->isCookiesSet()) {
				$this->deleteLoginDataCookies();
				$this->loginMessage = "Felaktig information i cookie"; 
			}
			else {
				$this->loginMessage = "Felaktigt användarnamn och/eller lösenord";
			}
		}
	}
	
	public function saveLoginData() {
		return isset($_POST[self::$saveLoginData]);
	}
	
	/**
	 * Logout the user.
	 */
	public function logoutUser() {
		$this->deleteLoginDataCookies();
		$this->loginMessage = "Du har nu loggats ut";
	}
	
	/**
	 * Set Cookies for username and encrypted password.
	 * @var Timestamp $endTime
	 */
	public function setLoginDataCookies($username, $password, $expireDate) {
		setcookie(self::$CookieUsername, $username, $expireDate);
		setcookie(self::$CookiePassword, $password, $expireDate);
	}
	
	private function deleteLoginDataCookies() {
		setcookie(self::$CookieUsername, "", time() -1);
		setcookie(self::$CookiePassword, "", time() -1);
	}
	
	public function isCookiesSet() {
		if(isset($_COOKIE[self::$CookieUsername]) && isset($_COOKIE[self::$CookiePassword])) {
			$this->username = $_COOKIE[self::$CookieUsername];
			$this->password = $_COOKIE[self::$CookiePassword];
		   	return true;
	    }
		return false;
	}
	
	/**
	 * Check if user input is not empty.
	 * @return Boolean
	 */
	public function isInputDataSet() {
		// Username check.
		if (!empty($_POST[self::$inputUsername])) {
			$this->username = $_POST[self::$inputUsername];
		}
		else {
			$this->loginMessage = "Användarnamn saknas";
			return false;
		}
		
		// Password check.
		if (!empty($_POST[self::$inputPassword])) {
			$this->password = $_POST[self::$inputPassword];
		}
		else {
			$this->loginMessage = "Lösenord saknas";
			return false;
		}
		
		return true;
	}
	
	/**
	 * @return String
	 */
	public function getUsername() {
		if ($this->isCookiesSet()) {
			return $_COOKIE[self::$CookieUsername];
		}
		return $this->username;
	}
	
	/**
	 * @return String
	 */
	public function getPassword() {
		if ($this->isCookiesSet()) {
			return $_COOKIE[self::$CookiePassword];
		}
		return $this->password;
	}
}
